home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Control del ratón / BetterBlockOut / BetterBlockOut.cs next >
Encoding:
Text File  |  2002-04-18  |  2.9 KB  |  93 lines

  1. //---------------------------------------------
  2. // BetterBlockOut.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class BetterBlockOut: Form
  9. {
  10.      bool      bBlocking, bValidBox;
  11.      Point     ptBeg, ptEnd;
  12.      Rectangle rectBox;
  13.  
  14.      public static void Main()
  15.      {
  16.           Application.Run(new BetterBlockOut());
  17.      }
  18.      public BetterBlockOut()
  19.      {
  20.           Text = "Rectßngulo mejorado";
  21.           BackColor = SystemColors.Window;
  22.           ForeColor = SystemColors.WindowText;
  23.      }
  24.      protected override void OnMouseDown(MouseEventArgs mea)
  25.      {
  26.           if (mea.Button == MouseButtons.Left)
  27.           {
  28.                ptBeg = ptEnd = new Point(mea.X, mea.Y);
  29.  
  30.                Graphics grfx = CreateGraphics();
  31.                grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
  32.                grfx.Dispose();
  33.  
  34.                bBlocking = true;
  35.           }
  36.      }
  37.      protected override void OnMouseMove(MouseEventArgs mea)
  38.      {
  39.           if (bBlocking && (mea.Button & MouseButtons.Left) != 0)
  40.           {
  41.                Graphics grfx = CreateGraphics();
  42.                grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd));
  43.                ptEnd = new Point(mea.X, mea.Y);
  44.                grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
  45.                grfx.Dispose();
  46.                Invalidate();
  47.           }
  48.      }
  49.      protected override void OnMouseUp(MouseEventArgs mea)
  50.      {
  51.           if (bBlocking)
  52.           {
  53.                Graphics grfx = CreateGraphics();
  54.                rectBox = Rect(ptBeg, new Point(mea.X, mea.Y));
  55.                grfx.DrawRectangle(new Pen(ForeColor), rectBox);
  56.                grfx.Dispose();
  57.  
  58.                bBlocking = false;
  59.                bValidBox = true;
  60.                Invalidate();
  61.           }
  62.      }
  63.      protected override void OnKeyPress(KeyPressEventArgs kpea)
  64.      {
  65.           if (bBlocking && kpea.KeyChar == '\x001B')   // Escape
  66.           {
  67.                Graphics grfx = CreateGraphics();
  68.                grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd));
  69.                grfx.Dispose();
  70.                
  71.                bBlocking = false;
  72.                Invalidate();
  73.           }
  74.      }
  75.      protected override void OnPaint(PaintEventArgs pea)
  76.      {
  77.           Graphics grfx = pea.Graphics;
  78.  
  79.           if (bValidBox)
  80.                grfx.FillRectangle(new SolidBrush(ForeColor), rectBox);
  81.  
  82.           if (bBlocking)
  83.                grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
  84.      }
  85.      Rectangle Rect(Point ptBeg, Point ptEnd)
  86.      {
  87.           return new Rectangle(Math.Min(ptBeg.X, ptEnd.X),
  88.                                Math.Min(ptBeg.Y, ptEnd.Y),
  89.                                Math.Abs(ptEnd.X - ptBeg.X),
  90.                                Math.Abs(ptEnd.Y - ptBeg.Y));
  91.      }
  92. }
  93.